type PlayerData = {
    name:string,
    position:GameVector3,
    last_pos:GameVector3,
    velocity:GameVector3,

    hp:number,

    mp:number,

    io:[{
        value:number,
        name:string
    }][]

    entitys:{
        player:GamePlayerEntity,

        body:{
            body:GameEntity,

            head:GameEntity,

            left_hand:GameEntity,
            right_hand:GameEntity,

            left_foot:GameEntity,
            right_foot:GameEntity,

            crama:GameEntity
        }

        bound:GameEntity

        gun:GameEntity
    }

    gun:{
        Bullet:number
    }
}

const entity_add_pos = {
    body:{
        body:new GameVector3(0,0,0),

        head:new GameVector3(0,0.56,0),

        left_hand:new GameVector3(-0.3,0,0),
        right_hand:new GameVector3(0.3,0,0),

        left_foot:new GameVector3(-0.1,-0.72,0),
        right_foot:new GameVector3(0.1,-0.72,0),

        crama:new GameVector3(0,0.6,0)
    },
    gun:new GameVector3(0,0,0)
}


const rpos = new GameVector3(128,128,128)
const max_hp = 100,max_mp = 100
const max_bullet = 8

const players:Player[] = []

export class Player
{
    data:PlayerData

    constructor(entity:GamePlayerEntity)
    {
        //方向为+Z
        this.data = {
            name:entity.player.name,
            position:rpos,
            last_pos:rpos,
            velocity:new GameVector3(0,0,0),
        
            hp:max_hp,
            mp:max_mp,

            io:[],

            entitys:{
                player:entity,

                body:{
                    body:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.4,0.72,0.2),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.body),
                    })!,
                    
                    head:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.4,0.4,0.4),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.head),

                        meshColor:new GameRGBAColor(0,0,1,1)
                    })!,

                    left_hand:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.2,0.72,0.2),
                        meshColor:new GameRGBAColor(0,1,0,1),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.left_hand),
                    })!,
                    right_hand:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.2,0.72,0.2),
                        meshColor:new GameRGBAColor(0,1,0,1),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.right_hand),
                    })!,

                    left_foot:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.2,0.72,0.2),
                        meshColor:new GameRGBAColor(0,1,0,1),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.left_foot),
                    })!,
                    right_foot:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(0.2,0.72,0.2),
                        meshColor:new GameRGBAColor(0,1,0,1),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.right_foot),
                    })!,

                    crama:world.createEntity({
                        mesh:'mesh/单位像素.vb',
                        meshScale:new GameVector3(1,1,1),
                        meshColor:new GameRGBAColor(0,0,0,0),

                        collides:false,
                        fixed:false,
                        gravity:false,

                        position:rpos.add(entity_add_pos.body.crama),
                    })!
                },
                
                bound:world.createEntity({
                    mesh:'mesh/单位像素.vb',
                    meshScale:new GameVector3(0.6,1.8,0.6),
                    meshColor:new GameRGBAColor(0,0,0,0.3),

                    collides:true,
                    fixed:false,
                    gravity:true,

                    position:rpos,
                })!,

                gun:world.createEntity({
                    mesh:'mesh/温彻斯特M1897霰弹枪.vb',
                    meshScale:new GameVector3(1/160,1/160,1/160),

                    collides:false,
                    fixed:false,
                    gravity:false,

                    position:rpos,
                })!
            },

            gun:{
                Bullet:max_bullet
            }
        }

        //look
        entity.player.cameraEntity = this.data.entitys.body.crama
        //entity.player.cameraMode = GameCameraMode.FPS
        entity.player.cameraFovY = 0.3
        entity.position.set(128.5,2,128.5)

        //add player to players
        players.push(this)
    }
}

const speed = 0.5 

world.onTick(({tick}) => {
    for(const e of players)
    {
        //
        e.data.position = e.data.entitys.bound.position

        const look_to = Math.PI/2+Math.atan2(e.data.entitys.player.player.facingDirection.z,e.data.entitys.player.player.facingDirection.x)
        const look = new GameQuaternion(0,0,0,1).rotateY(look_to)
        //
    
        if(e.data.entitys.player.player.walkState != GamePlayerWalkState.NONE)
        {
            const speed2 = e.data.entitys.player.player.walkState == GamePlayerWalkState.CROUCH || e.data.entitys.player.player.action1Button?speed*0.3:speed
            e.data.entitys.bound.velocity.x = e.data.entitys.player.player.facingDirection.x*speed2
            e.data.entitys.bound.velocity.z = e.data.entitys.player.player.facingDirection.z*speed2

            if(tick%4 == 0)
            {
                e.data.entitys.bound.sound('audio/step.mp3')
            }
            
            
        }
        else
        {
            e.data.entitys.bound.velocity.x = 0
            e.data.entitys.bound.velocity.z = 0
        }

        {
            const t = Math.PI/4*(Math.abs(tick%200-100)-50)/50
            const y = Math.sin(t)*0.72

            if(tick%5 == 0)
                console.log(t)

            const add_pos1 = entity_add_pos.body.left_foot
            const add_pos2 = entity_add_pos.body.right_foot

            const look_to2 = look_to+Math.PI/2

            e.data.entitys.body.left_foot.position = e.data.position.add(
                new GameVector3(
                    Math.cos(look_to)*add_pos1.x,
                    add_pos1.y,
                    Math.sin(look_to)*add_pos1.x
                )
            ).add(
                new GameVector3(
                    Math.cos(look_to2)*y,
                    y,
                    Math.sin(look_to2)*y
                )
            )

            e.data.entitys.body.right_foot.position = e.data.position.add(
                new GameVector3(
                    Math.cos(look_to)*add_pos2.x,
                    add_pos2.y,
                    Math.sin(look_to)*add_pos2.x
                )
            ).add(
                new GameVector3(
                    Math.cos(look_to2)*-y,
                    y,
                    Math.sin(look_to2)*-y
                )
            )
            // 创建基础方向旋转 (Y轴)
            const baseLook = new GameQuaternion(0,0,0,1).rotateY(look_to)

            // 分别创建左右腿旋转量 (X轴正负交替)
            const leftFootRot = new GameQuaternion(0,0,0,1).rotateX(t)
            const rightFootRot = new GameQuaternion(0,0,0,1).rotateX(-t)

            // 组合基础方向和腿部旋转
            e.data.entitys.body.left_foot.meshOrientation = baseLook.mul(leftFootRot)
            e.data.entitys.body.right_foot.meshOrientation = baseLook.mul(rightFootRot)

        }


        e.data.velocity = e.data.entitys.bound.position.sub(e.data.last_pos)

        e.data.last_pos = e.data.entitys.bound.position.clone()

        for(const key of Object.keys(e.data.entitys.body))
        {
            //@ts-expect-error
            const es = e.data.entitys.body[key] as GameEntity,add_pos = entity_add_pos.body[key] as GameVector3
                
            es.velocity = e.data.velocity
            if(key != 'right_foot' && key != 'left_foot')
            {
                es.position = e.data.position.add(
                    new GameVector3(
                        Math.cos(look_to)*add_pos.x,
                        add_pos.y,
                        Math.sin(look_to)*add_pos.x
                    )
                )
                es.meshOrientation = look.clone()
            }
        }
    }
})